package com.yx.myinterest;

import android.app.Dialog;
import android.content.Context;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.yx.myinterest.log.LogUtil;

/**
 * 继承Dialog弹出透明输入框
 * DialogContentOperation dialogCommon = new DialogContentOperation(this);
 * dialogCommon.showInputDialog("内容", "内容");
 * dialogCommon.setOnGetInputMsgListener(msg -> {
 * <p>
 * });
 */
public class DialogContentOperation extends Dialog {
    static {
        /**
         * 忽略Https
         * 校验
         */
        // HttpsignoreUtil.ignore();
    }

    public interface OnGetInputMsgListener {
        void onGetInputMsg(String msg);
    }

    private float startY;
    private float moveY = 0;
    private View decorView;

    private TextView tvCancel;
    private TextView tvTitle;
    private LinearLayout layoutDisLike, layoutLike;
    private Vibrator vibrator; // 震动
    private OnGetInputMsgListener onGetInputMsgListener;

    public void setOnGetInputMsgListener(OnGetInputMsgListener onGetInputMsgListener) {
        this.onGetInputMsgListener = onGetInputMsgListener;
    }

    public DialogContentOperation(@NonNull Context context) {
        super(context, R.style.DialogCommon);
        setContentView(R.layout.dialog_content_operation);
        Window w = getWindow();
        if (w != null) {
            w.setGravity(Gravity.BOTTOM); // 内容居下。
            w.setWindowAnimations(R.style.mystyle); // 设置动画
            decorView = getWindow().getDecorView();
            WindowManager.LayoutParams lp = w.getAttributes();
            DisplayMetrics d = context.getResources().getDisplayMetrics();  //  获取屏幕宽、高用
            lp.width = d.widthPixels;
            w.setAttributes(lp);
        }
        tvCancel = findViewById(R.id.dialog_co_cancel);
        tvCancel.setOnClickListener(clicks);
        tvTitle = findViewById(R.id.dialog_co_text);
        layoutDisLike = findViewById(R.id.dialog_co_dislike);
        layoutDisLike.setOnClickListener(clicks);
        layoutLike = findViewById(R.id.dialog_co_like);
        layoutLike.setOnClickListener(clicks);
        vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }


    public void showDialog(String title, String aid) {
        // etInput.setInputType(InputType.TYPE_CLASS_NUMBER);
        // etInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);允许输入数字和小数
        // shareText.setInputType(InputType.TYPE_CLASS_TEXT);
        // this.onGetInputMsgListener = listener;
        /*
         * 对于 pattern ,如有定义为new int[200,400,600,800]
         * 200：指示在打开振动器之前等待的毫秒数。 0.2秒。
         * 400：表示在关闭振动器之前保持振动器处于开启状态的毫秒数，0.4秒。
         * 600,800：几毫秒之间交替，以关闭振动器或打开振动器，0.6-0.8秒。
         */
        long[] pattern = {200, 50}; // 震动时间，或vibrator.vibrate(2000); 震动2秒。
        // 有规律震动,-1表示不重复，0表示循环震动
        vibrator.vibrate(pattern, -1);
        tvTitle.setText(title);
        show();
    }

    public View.OnClickListener clicks = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialog_co_cancel:
                    dismiss();
                    break;
                case R.id.dialog_co_dislike:
                    Toast.makeText(getContext(), "将为您减少此类推荐", Toast.LENGTH_LONG).show();
                    dismiss();
                    break;
                case R.id.dialog_co_like:
                    Toast.makeText(getContext(), "知识+1", Toast.LENGTH_LONG).show();
                    dismiss();
                    break;
            }
        }
    };

    @Override
    public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                LogUtil.iYx("--moveY：" + ev.getY() + "  /  " + startY);
                moveY = ev.getY() - startY;

                if (moveY > 0) { // 下滑 避免坐标抖动，滑动过程中闪烁不连贯的问题
                    decorView.scrollBy(0, -(int) moveY);
                    startY = ev.getY();
                } else { // 上滑
                    decorView.scrollBy(0, (int) Math.abs(moveY));
                    startY = ev.getY();
                }
                if (decorView.getScrollY() > 0) { // 避免向上拖动
                    decorView.scrollTo(0, 0);
                }

                break;
            case MotionEvent.ACTION_UP:
                if (decorView.getScrollY() < -decorView.getHeight()
                        / 5 && moveY > 0) { // 如果滑动距离达到高度的5分之一 就关闭Dialog
                    this.dismiss();
                }
                decorView.scrollTo(0, 0);
                break;
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void dismiss() {
        if (vibrator != null) {
            vibrator.cancel();
        }
        super.dismiss();
    }


}
